package com.zhanjixun.weixin.utils;

import lombok.extern.log4j.Log4j;
import okio.BufferedSink;
import okio.Okio;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLEncoder;

/**
 * 工具类
 *
 * @author :zhanjixun
 * @date : 2018/10/25 21:41
 */
@Log4j
public class U {

    private static final String TEMP_DIR = System.getProperty("java.io.tmpdir");

    public static File newFile(String path) {
        File file = new File(path);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        return file;
    }

    public static File downloadFile(byte[] data, String fileName) {
        File file = new File(TEMP_DIR, fileName);
        try {
            BufferedSink buffer = Okio.buffer(Okio.sink(file));
            buffer.writeAll(Okio.source(new ByteArrayInputStream(data)));
            buffer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static String downloadImage(BufferedImage image, String fileName) {
        try {
            File output = new File(TEMP_DIR, fileName);
            ImageIO.write(image, StringUtils.substringAfterLast(fileName, "."), output);
            return output.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String base64Image(String imageBase64, String fileName) {
        try {
            imageBase64 = StringUtils.substringAfter(imageBase64, "data:img/jpg;base64,");
            byte[] data = new BASE64Decoder().decodeBuffer(imageBase64);
            for (int i = 0; i < data.length; ++i) {
                if (data[i] < 0) {
                    data[i] += 256;
                }
            }
            File file = new File(TEMP_DIR, fileName);

            BufferedSink buffer = Okio.buffer(Okio.sink(file));
            buffer.writeAll(Okio.source(new ByteArrayInputStream(data)));
            buffer.flush();

            log.info("下载图片：" + file.getAbsolutePath());
            return file.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String urlEncode(String content) {
        try {
            return URLEncoder.encode(content, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        throw new RuntimeException();
    }

    public static String randomDeviceID() {
        return "e" + RandomStringUtils.randomNumeric(15);
    }

    public static String r() {
        return RandomStringUtils.randomNumeric(10);
    }

    public static String timeMillis() {
        return String.valueOf(System.currentTimeMillis());
    }

    public static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static byte[] readFile(File file, int from, int to) {
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
            randomAccessFile.seek(from);
            byte[] bytes = new byte[to - from];
            randomAccessFile.read(bytes);
            return bytes;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] readFile(File file) {
        try {
            return Okio.buffer(Okio.source(file)).readByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
